﻿using System;
using System.ComponentModel;
using System.Reflection;

namespace CaltechSoft.Service.Infrastructure.Utility
{
    public class LocalizedDescriptionAttribute : DescriptionAttribute
    {
        private PropertyInfo _resourceProperty;

        public LocalizedDescriptionAttribute()
        {
        }

        public LocalizedDescriptionAttribute(string description)
            : base(description)
        {
        }

        public string DescriptionResourceKey { get; set; }
        public Type DescriptionResourceType { get; set; }

        public override string Description
        {
            get
            {
                if (_resourceProperty == null) _resourceProperty = DescriptionResourceType.GetProperty(DescriptionResourceKey, BindingFlags.Static | BindingFlags.Public);
                if (_resourceProperty == null) return base.Description;
                return (string)_resourceProperty.GetValue(_resourceProperty.DeclaringType, null);
            }
        }
    }
}